package com.mskj.zxmy.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mskj.zxmy.dto.ProductDetailDTO;
import com.mskj.zxmy.dto.ReportQueryDTO;
import com.mskj.zxmy.entity.ReportMain;
import com.mskj.zxmy.entity.SctechMissions;
import com.mskj.zxmy.mapper.MaterialInfoMapper;
import com.mskj.zxmy.mapper.ReportMapper;
import com.mskj.zxmy.mapper.StockInMapper;
import com.mskj.zxmy.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private StockInMapper stockInMapper;

    @Autowired
    private MaterialInfoMapper materialInfoMapper;


    @Override
    public PageInfo<ReportMain> getReportListByQuery(ReportQueryDTO query) {
        // 1. 如果有报工单号，直接查报工表
        if (query.getReportNumber() != null && !query.getReportNumber().isEmpty()) {
            PageHelper.startPage(query.getPageNum(), query.getPageSize());
            List<ReportMain> results = reportMapper.selectReportListByReportNumber(query.getReportNumber());
            for (ReportMain result : results) {
                extracted(result);
            }
            return new PageInfo<>(results);
        }

        // 2. 如果有其他筛选条件，先获取筛选后的产品ID列表
        if (query.getProductName() != null || query.getProductSpecifications() != null ||
                query.getMaterialCategory() != null || query.getBrand() != null) {

            // 先查物料信息表id（或productNumber）
            List<String> productIds = materialInfoMapper.selectProductNumbersByFilter(
                    query.getProductName(),
                    query.getProductSpecifications(),
                    query.getMaterialCategory(),
                    query.getBrand()
            );
            if (productIds == null || productIds.isEmpty()) {
                return new PageInfo<>(Collections.emptyList());
            }

            // 设置筛选后的产品ID列表到查询对象中
            query.setProductIds(productIds);

            // 使用新的筛选查询方法，在数据库层面进行分页
            PageHelper.startPage(query.getPageNum(), query.getPageSize());
            List<ReportMain> results = reportMapper.selectReportListWithFilter(query);
            for (ReportMain result : results) {
                extracted(result);
            }
            return new PageInfo<>(results);
        }

        // 3. 如果没有筛选条件，使用通用查询
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<ReportMain> results = reportMapper.getReportList(query);
        for (ReportMain result : results) {
            extracted(result);
        }
        return new PageInfo<>(results);
    }

    //    查询物料类别
    @Override
    public List<String> getCategoryList() {
        return reportMapper.getCategoryList();
    }

    @Override
    public List<String> getBrandList() {
        return reportMapper.getBrandList();
    }

    private void extracted(ReportMain result) {
        result.setReportingStaff(stockInMapper.getUserNameBymenmberId(result.getReportingStaff()));
        result.setSynchronisingFolderFailedSS(stockInMapper.getUserNameBymenmberId(result.getSynchronisingFolderFailedSS()));
        ProductDetailDTO productDetail = stockInMapper.getProductInfoByNumber(result.getProductInformation());
        result.setProductInfo(productDetail.getProductNumber() + " " + productDetail.getProductName() + " " + productDetail.getMaterialCategory());
        result.setAssociatedProductionTasks(reportMapper.getAssociatedProductionTasks(result.getAssociatedProductionTasks()));
        SctechMissions processInfo = reportMapper.getLinkProcessTasks(result.getLinkProcessTasks());
        result.setLinkProcessTasks(processInfo.getCraftFlowNumber());
        result.setProcessName(processInfo.getProcessName());
    }

    /**
     * 报工列表查询
     *
     * @return reportMains
     */
    @Override
    public PageInfo<ReportMain> getReportList(ReportQueryDTO query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        List<ReportMain> results = reportMapper.getReportList(query);
        for (ReportMain result : results) {
            extracted(result);
        }
        return new PageInfo<>(results);
    }

    @Override
    public ReportMain getReportDetail(Integer id) {
        return reportMapper.selectReportById(id);
    }

}